package com.synergy.stockflow.procure.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.entity.SysUser;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.procure.entity.SupplierPrices;
import com.synergy.stockflow.procure.entity.Vendor;
import com.synergy.stockflow.procure.service.VendorService;
import com.synergy.stockflow.sales.entity.Quotation;
import com.synergy.stockflow.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vendor")
public class VendorController {

    @Autowired
    VendorService vendorService;

    @Autowired
    SysUserService sysUserService;

    @GetMapping
    public Result<Page<Vendor>> getAllVendors(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "vendorName", required = false) String vendorName,
            @RequestParam(name = "vendorGrade", required = false) String vendorGrade,
            @RequestParam(name = "vendorSettlementperiod", required = false) String vendorSettlementperiod,
            @RequestParam(name = "vendorHeadofpurchasing", required = false) String vendorHeadofpurchasing,
            @RequestParam(name = "vendorContactphone", required = false) String vendorContactphone){
        Page<Vendor> page = new Page<>(current, size);
        page = vendorService.getVendor(page, vendorName, vendorGrade, vendorSettlementperiod, vendorHeadofpurchasing, vendorContactphone);
        for(Vendor v:page.getRecords()){
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            v.setNickname(sysUserService.getOne(queryWrapper.eq(SysUser::getId,v.getVendorHeadofpurchasing())).getNickname());
        }
        return Result.success(page);
    }
    @PostMapping
    public Result insert(@RequestBody Vendor vendor) {
        vendorService.save(vendor);
        return Result.success(vendor);
    }

    @PutMapping
    public Result update(@RequestBody Vendor vendor) {
        vendorService.updateById(vendor);
        System.out.println(vendor);
        return Result.success(vendor);
    }
    @DeleteMapping
    public Result delete(@RequestBody List<Vendor> vendor) {
        for(Vendor v : vendor) {
            vendorService.removeById(v.getVendorId());
        }
        return Result.success(vendor);
    }

    @GetMapping("/All")
    public Result<List<Vendor>> getAllSupplierPricesController(){
        List<Vendor> list = vendorService.list();
        return Result.success(list);
    }

}
